// pages/companyList/companyList.js
import api from "../../api/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    test:"",
    driverName:"",
    show:false,
    radio: '',
    companyid:""
  },
  //设置司机名字
  setDrivername(){
    let that = this
    let id = wx.getStorageSync('id')
    api.post('/companyInfoApp/getDriverName', {id:id,driverName:that.data.driverName})
    .then(res => {
    })
  },
  gotoHomepage() {
    let that = this
    let id = wx.getStorageSync('id')
    if (!/^[\u4E00-\u9FA5]{2,10}(·[\u4E00-\u9FA5]{2,10}){0,2}$/.test(that.data.driverName)) {
      wx.showToast({
        icon:'none',
        title: '姓名格式出错',
        duration: 2000//持续的时间
      })
    } else {
      api.post('/appLogin/selectCompany', {id:id,companyId:that.data.companyid})
      that.setDrivername()
      wx.setStorageSync('companyid', that.data.companyid)
      wx.switchTab({
        url: '/pages/homePage/homePage'
      })
    }
  },
  gotoOrder(){
    let that=this
    if (that.data.companyid) {
      that.setdriver()
      that.setData({
        test:"",
        show:true
      })
    } else {
      wx.showToast({
        icon:'none',
        title: "请选择物流公司",
        duration: 2000
      })
    }
  },
  onChangename(event) {
    let that = this
    that.setData({
      driverName:event.detail
    })
  },
//修改司机状态
setdriver(){
  let that = this;
  let id= wx.getStorageSync('id')
  api.post('/companyInfoApp/updateDriver', {id:id})
  .then(res => {
    that.isDriver(id)
  })
  .catch(err => {
  wx.showToast({
    icon:'none',
    title: err.message,
    duration: 2000
  })
})
},
//是否是司机
isDriver(id){
  try {
    api.post('/companyInfoApp/findDriverStatus', {id:id})
    .then(res => {
      wx.setStorageSync('isdriver', res.data.isDriver)
    })
   } catch (error) {
     console.log("error")
   }
},
//获取订单详情
getcompanylist() {
  // 改变this指向
  let that = this;
  let id= wx.getStorageSync('id')
  api.post('/deliver/pageCompanyInformation', {id:id})
  .then(res => {
    that.setData({
      companylist:res.data,
    })
  })
  .catch(err => {
  wx.showToast({
    icon:'none',
    title: err.message,
    duration: 2000
  })
})
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    that.getcompanylist()
  },

  choosecompany(data){
    let that=this
    let id = data.currentTarget.dataset.index
    that.setData({
      companyid:id
    })
    console.log(that.data.companyid);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})